import React, { Component, Suspense } from 'react';
import { BrowserRouter, Route, Switch, Redirect } from 'react-router-dom';
import { ConnectedRouter } from 'connected-react-router';
import history from "@/store/history";
import Loading from "@/component/loading";
import { getCookie } from '@/utils';

import NotFound from '../page/_404';
import Index from '../page/index';
import Login from "../page/Login";
import Layout from '../page/user/Layout';

export default class Router extends Component{
    render() {
        return (
            <section className="section">
                <Suspense fallback={<Loading />}>
                    {/*<BrowserRouter>*/}
                    <ConnectedRouter history={history}>
                        <Switch>
                            <Route exact path="/" render={(props) => {
                                document.title = '欢迎访问51实训系统';
                                return <Index {...props} />
                            }}/>
                            <Route exact path="/login" render={(props) => {
                                document.title = '用户登录';
                                return <Login {...props} />
                            }}/>
                            <Route path="/user" render={(props) => {
                                if(!getCookie('USER_TOKEN')){
                                    return <Redirect to={"/login?redirect="+encodeURIComponent(props.location.pathname)}/>
                                }
                                return <Layout {...props} />
                            }}/>
                            <Route path="*" render={(props) => {
                                document.title = '404 页面不存在';
                                return <NotFound {...props} />
                            }}/>
                        </Switch>
                    </ConnectedRouter>
                    {/*</BrowserRouter>*/}
                </Suspense>
            </section>
        )
    }
}
